#region The MIT License
// The MIT License
// 
// Copyright (c) 2009 octalforty studios
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#endregion
using System;
using System.Diagnostics;
using octalforty.Componento.Shared.Util;

namespace octalforty.Componento.Shared.SemanticModel
{
    /// <summary>
    /// Represents a single entry in Componento Repository.
    /// </summary>
    [DebuggerDisplay("Name: {Name}, Version: {Version}, Manifest Uri: {ManifestUri}, Project Uri: {ProjectUri}")]
    public sealed class ComponentDescriptor : IEquatable<ComponentDescriptor>
    {
        #region Private Fields
        private readonly string name;
        private readonly Version version;
        private readonly Uri manifestUri;
        private readonly Uri projectUri;
        private readonly Uri projectLogoUri;
        private readonly string description;
        private readonly CliEnvironment[] clis = new CliEnvironment[] { };
        private readonly IsaType[] isas = new IsaType[] { };
        private readonly string[] licenses = new string[] { };
        #endregion

        #region Public Properties
        /// <summary>
        /// Gets the name of the Component.
        /// </summary>
        public string Name
        {
            [DebuggerStepThrough]
            get { return name; }
        }

        /// <summary>
        /// Gets the version of the Component.
        /// </summary>
        public Version Version
        {
            [DebuggerStepThrough]
            get { return version; }
        }

        /// <summary>
        /// Gets an URI of the Component Manifest.
        /// </summary>
        public Uri ManifestUri
        {
            [DebuggerStepThrough]
            get { return manifestUri; }
        }

        /// <summary>
        /// Gets an URI of the project.
        /// </summary>
        public Uri ProjectUri
        {
            [DebuggerStepThrough]
            get { return projectUri; }
        }

        /// <summary>
        /// Gets an URI of the project logo.
        /// </summary>
        public Uri ProjectLogoUri
        {
            [DebuggerStepThrough]
            get { return projectLogoUri; }
        }

        /// <summary>
        /// Gets the description of the Component.
        /// </summary>
        public string Description
        {
            [DebuggerStepThrough]
            get { return description; }
        }

        /// <summary>
        /// Gets an array of <see cref="CliEnvironment"/> objects, which contain information about CLI
        /// implementation this Component supports.
        /// </summary>
        public CliEnvironment[] Clis
        {
            [DebuggerStepThrough]
            get { return clis; }
        }

        /// <summary>
        /// Gets an array of <see cref="IsaType"/> members, which represent which ISAs
        /// this component supports.
        /// </summary>
        public IsaType[] Isas
        {
            [DebuggerStepThrough]
            get { return isas; }
        }

        /// <summary>
        /// Gets an array of strings, which represent names of licenses this component
        /// is licensed under.
        /// </summary>
        public string[] Licenses
        {
            get { return licenses; }
        }
        #endregion

        /// <summary>
        /// Initializes a new instance of the <see cref="ComponentDescriptor"/> class.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="version"></param>
        /// <param name="manifestUri"></param>
        /// <param name="projectUri"></param>
        /// <param name="description"></param>
        [DebuggerStepThrough]
        public ComponentDescriptor(string name, Version version, Uri manifestUri, Uri projectUri, string description)
        {
            this.name = name;
            this.description = description;
            this.version = version;
            this.projectUri = projectUri;
            this.manifestUri = manifestUri;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ComponentDescriptor"/> class.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="version"></param>
        /// <param name="manifestUri"></param>
        /// <param name="projectUri"></param>
        /// <param name="projectLogoUri"></param>
        /// <param name="description"></param>
        /// <param name="clis"></param>
        /// <param name="isas"></param>
        /// <param name="licenses"></param>
        [DebuggerStepThrough]
        public ComponentDescriptor(string name, Version version, Uri manifestUri, Uri projectUri, Uri projectLogoUri, string description, 
                                   CliEnvironment[] clis, IsaType[] isas, string[] licenses)
        {
            this.name = name;
            this.version = version;
            this.manifestUri = manifestUri;
            this.projectUri = projectUri;
            this.projectLogoUri = projectLogoUri;
            this.description = description;
            this.clis = clis;
            this.isas = isas;
            this.licenses = licenses;
        }

        #region IEquatable<ComponentDescriptor> Members
        /// <summary>
        /// Indicates whether the current object is equal to another object of the same type.
        /// </summary>
        /// <returns>
        /// true if the current object is equal to the other parameter; otherwise, false.
        /// </returns>
        /// <param name="other">An object to compare with this object.</param>
        public bool Equals(ComponentDescriptor other)
        {
            if(ReferenceEquals(null, other)) return false;
            if(ReferenceEquals(this, other)) return true;
            return Equals(other.name, name) && Equals(other.version, version) && Equals(other.manifestUri, manifestUri) && 
                   Equals(other.projectUri, projectUri) && Equals(other.description, description) && 
                   ArrayUtil.Equals(other.clis, clis) &&
                   ArrayUtil.Equals(other.isas, isas) &&
                   ArrayUtil.Equals(other.licenses, licenses);
        }

        #endregion

        #region Object Members
        /// <summary>
        /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
        /// </returns>
        /// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>. </param><filterpriority>2</filterpriority>
        public override bool Equals(object obj)
        {
            if(ReferenceEquals(null, obj)) return false;
            if(ReferenceEquals(this, obj)) return true;
            if(obj.GetType() != typeof(ComponentDescriptor)) return false;
            return Equals((ComponentDescriptor)obj);
        }

        /// <summary>
        /// Serves as a hash function for a particular type. <see cref="M:System.Object.GetHashCode"/> is suitable for use in hashing algorithms and data structures like a hash table.
        /// </summary>
        /// <returns>
        /// A hash code for the current <see cref="T:System.Object"/>.
        /// </returns>
        /// <filterpriority>2</filterpriority>
        public override int GetHashCode()
        {
            unchecked
            {
                int result = (name != null ? name.GetHashCode() : 0);
                result = (result * 397) ^ (version != null ? version.GetHashCode() : 0);
                result = (result * 397) ^ (manifestUri != null ? manifestUri.GetHashCode() : 0);
                result = (result * 397) ^ (projectUri != null ? projectUri.GetHashCode() : 0);
                result = (result * 397) ^ (description != null ? description.GetHashCode() : 0);
                result = (result * 397) ^ (clis != null ? clis.GetHashCode() : 0);
                result = (result * 397) ^ (isas != null ? isas.GetHashCode() : 0);
                result = (result * 397) ^ (licenses != null ? licenses.GetHashCode() : 0);
                return result;
            }
        }

        public static bool operator ==(ComponentDescriptor left, ComponentDescriptor right)
        {
            return Equals(left, right);
        }

        public static bool operator !=(ComponentDescriptor left, ComponentDescriptor right)
        {
            return !Equals(left, right);
        }
        
        public override string ToString()
        {
            return string.Format("Name: {0}, Version: {1}, Manifest Uri: {2}", name, version, manifestUri);
        }
        #endregion
    }
}